package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.Menu;
import com.itheima.service.MenuService;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * ClassName: MenuController
 * Description:
 * date: 2019/12/1 11:13
 * Author Airson
 * Since JDK 1.8
 */
@RestController
@RequestMapping("/menu")
public class MenuController {

    @Reference
    MenuService menuService;


    @RequestMapping("/add")
    // @PreAuthorize("hasAuthority('CHECKITEM_ADD')")
    public Result add(@RequestBody Menu menu) {

        System.out.println(menu);
        try {
            menuService.add(menu);
            return new Result(true, "菜单添加成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, "菜单添加失败");

        }
    }

    @RequestMapping("/findPage")
    //@PreAuthorize("hasAuthority('CHECKITEM_QUERY')")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean) {
        return menuService.queryPage(queryPageBean);
    }


    @RequestMapping("/delById")
    //@PreAuthorize("hasAuthority('CHECKITEM_DELETE')")
    public Result delById(Integer id) {
        try {
            menuService.delById(id);
            return new Result(true, "删除菜单成功");
        } catch (RuntimeException e) {
            e.printStackTrace();
            return new Result(false, e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, "删除菜单失败");
        }
    }

    @RequestMapping("/findById")
    //@PreAuthorize("hasAuthority('CHECKITEM_QUERY')")
    public Result findById(Integer id) {
        try {
            Menu menu = menuService.findById(id);
            return new Result(true, "查询菜单成功", menu);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, "查询菜单失败");
        }
    }

    @RequestMapping("/edit")
    //@PreAuthorize("hasAuthority('CHECKITEM_EDIT')")
    public Result edit(@RequestBody Menu menu) {
        try {
            menuService.edit(menu);
            return new Result(true, "菜单更改成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, "菜单更改失败");
        }
    }


    @RequestMapping("/findAll")
    //@PreAuthorize("hasAuthority('CHECKITEM_QUERY')")
    public Result findAll() {
        try {
            List<Menu> menuList = menuService.findAll();
            return new Result(true, "查询菜单成功", menuList);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, "查询菜单失败");
        }
    }

}
